﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Controls;
using System.Windows;
using static MyMusicPlay._4._0.Media;
using System.Windows.Input;
using System.Windows.Media.Animation;

namespace MyMusicPlay._4._0
{
    /// <summary>
    /// 播放列表板
    /// </summary>
    public class MusicPlayingBoard : ViewBoard
    {
        private MainWindow _main;
        private Media _media;
        private RightMenu _rightMenu;
        private StackPanel _Board;//显示当前播放的元素
        private StackPanel _BoardAll;//显示当前播放列表的全部歌曲
        private StackPanel _BoardHeight;//控制板的主题显示板,主要控制高度
        private ScrollViewer _MusicPlayBoard_Scr;//滚动面板
        private Dictionary<int, Border> _dpList = new();//[Key,Border]
        private bool IsOne = true;
        public Border curPlayData;

        private OrderState _RecordPlsyState;//记录当前显示时的播放状态的状态
        private int _PlayingCount;//记录当前播放的歌曲树
        public MusicPlayingBoard(MainWindow main) : base(main)
        {
            _main = main;
        }

        public override void Initialize()
        {
            _media = PlayApplication.GetModule<Media>();
            _rightMenu = PlayApplication.GetModule<RightMenu>();
            _Board = _main.MusicPlayBoard;
            _BoardAll = _main.MusicPlayBoardAllList;
            _BoardHeight = _main.PlayBoardHeightSet;
            _MusicPlayBoard_Scr = _main.MusicPlayBoard_Scr;
        }

        public override void Show(ViewBoard child = null)
        {
            var board = PlayApplication.GetModule<MusicPlayingBoard>();
            if (board.IsChange())
            {
                board.Create();
            }

            var height = board.GetItemHeight();
            var curHeight = height + 5 + 1 + 40 + 5 + 5;
            if (curHeight > _BoardHeight.MaxHeight)
            {
                SY_Library.Animate.Deformation(_BoardHeight, SY_Library.Animate.direction.Height, _BoardHeight.Height, 400, 200);
            }
            else
            {
                SY_Library.Animate.Deformation(_BoardHeight, SY_Library.Animate.direction.Height, _BoardHeight.Height, curHeight, 200);
            }

            //设置滚动条高度
            var index = _media.GetPlayOrderList().IndexOf(_media.CurPlayingKey);
            if (index != -1)
            {
                _MusicPlayBoard_Scr.ScrollToVerticalOffset(GetItemHeight(index));
            }

            base.Show(this);
        }

        public override void Hidden()
        {
            SY_Library.Animate.Deformation(_BoardHeight, SY_Library.Animate.direction.Height, _BoardHeight.ActualHeight, 42, 200);
            base.Hidden();
        }

        /// <summary>
        /// 创建[此处会自动读取Media中的播放列表][注意:此处没有额外的歌曲列表,直接调用的播放器中的列表]
        /// </summary>
        public void Create()
        {
            if (IsOne)
            {
                var border = _main.PlayMusicView;
                SY_Library.Animate.Deformation(border, SY_Library.Animate.direction.Width, 0, 250, 200);
                IsOne = false;
            }
            _RecordPlsyState = _media.orderState;
            _PlayingCount = _media.CurPlayingList.Count;
            CreateMusicItem();
        }

        /// <summary>
        /// 获取Item的总高度
        /// </summary>
        public double GetItemHeight()
            => _media.CurPlayingList.Count * 49;
        public double GetItemHeight(int count)
            => count * 49;


        /// <summary>
        /// 是否进行了改变[需要重新渲染背板]
        /// </summary>
        public bool IsChange()
        {
            if (_RecordPlsyState != _media.orderState)
            {
                return true;
            }
            if (_PlayingCount != _media.CurPlayingList.Count)
            {
                return true;
            }
            return false;
        }

        /// <summary>
        /// 修改显示当前的面板
        /// </summary>
        public void SetTopBoard(Music music)
        {
            PlayApplication.GetModule<Rander>().CreatePlayingBoardByTop(_Board, music);

            var data = _media.CurPlayingList.FirstOrDefault(u => u.Value.Id == music.Id);
            //清空指针颜色
            if (curPlayData != null)
            {
                curPlayData.BorderBrush = SY_Library.Color.BackColor(255);
            }
            if (data.Value is not null)
            {
                curPlayData = _dpList[data.Key];
                curPlayData.BorderBrush = SY_Library.Color.BackColor(103, 58, 183);
            }
        }

        /// <summary>
        /// 删除选择的音乐
        /// </summary>
        public void DelMusicPlaying(int key)
        {
            Console.WriteLine($"删除Key:{key}");
            PlayApplication.GetModule<MusicPlaying>().DelCurPlayingMusic(key);
            if (_dpList.ContainsKey(key))
            {
                var cur = _dpList[key];
                SY_Library.Animate.Deformation(cur, SY_Library.Animate.direction.Width, cur.ActualWidth, 0, 200, (u) => { u.EasingFunction = new ExponentialEase() { EasingMode = EasingMode.EaseInOut }; });
                SY_Library.Animate.Deformation(cur, SY_Library.Animate.direction.Height, cur.ActualHeight, 0, 100, (u) => { u.EasingFunction = new ExponentialEase() { EasingMode = EasingMode.EaseInOut }; });
                Task.Run(() =>
                {
                    Task.Delay(300).Wait();
                    _BoardAll.Children.Remove(cur);
                    _dpList.Remove(key);
                });
            }
        }

        /// <summary>
        /// 刷新选择的音乐
        /// </summary>
        public void FlushedChooseMusic(Music music)
        {
            //判断列表中是否有
        }

        private void CreateMusicItem()
        {
            _BoardAll.Children.Clear();
            _dpList.Clear();
            var randomList = _media.GetPlayOrderList();
            var curPlayList = _media.CurPlayingList;
            foreach (var item in randomList)
            {
                if (curPlayList.ContainsKey(item))
                {
                    var border = PlayApplication.GetModule<Rander>().CreateItemByMusicPlayingBoard(curPlayList[item], item);
                    _BoardAll.Children.Add(border);
                    _BoardAll.Height = GetItemHeight();
                    _dpList.Add(item, border);
                }
            }
        }

        private class BoardElemate
        {
            public Border Border { get; set; }
            public DockPanel DockPanel { get; set; }
            public BoardElemate(Border bor, DockPanel doc)
                => (Border, DockPanel) = (bor, doc);
        }
    }
}
